﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MAS.Web.UI.WebControls
{
    public partial class Gauge : System.Web.UI.UserControl
    {
        public decimal? TargetValue { get; set; }
        public decimal ActualValue { get; set; }
        public decimal? AlarmValue { get; set; }

        public string Title { get; set; }

        private int _Width = 250;
        public int Width
        {
            get { return _Width; }
            set { _Width = value; }
        }

        private int _Height = 250;
        public int Height
        {
            get { return _Height; }
            set { _Height = value; }
        }

        public string GaugeUnit { get; set; }

        protected decimal min = 0, max = 0, step = 0;
        protected bool upGoing = false;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void BindGauge()
        {
            if (!AlarmValue.HasValue && !TargetValue.HasValue && ActualValue == 0)
                return;

            decimal target = TargetValue.HasValue ? TargetValue.Value : 0,
                alarm = AlarmValue.HasValue ? AlarmValue.Value : 0;

            min = Math.Min(target, alarm);
            max = Math.Max(target, alarm);

            min = Math.Min(min, ActualValue);
            max = Math.Max(max, ActualValue);

            upGoing = alarm < target;
            step = (max - min) / 1;

            decimal redBegin = 0, redEnd = 0, yellowBegin = 0, yellowEnd = 0, greenBegin = 0, greenEnd = 0;
            if (upGoing)
            {
                redBegin = min - step;
                yellowBegin = redEnd = alarm;
                greenBegin = yellowEnd = target;
                greenEnd = max + step;
            }
            else
            {
                greenBegin = min - step;
                greenEnd = yellowBegin = target;
                yellowEnd = redBegin = alarm;
                redEnd = max + step;
            }

            string script = @"var option_" + ClientID + @" = {
        chart: {
	            renderTo: 'content_" + innerId + @"',
	            type: 'gauge',
	            plotBackgroundColor: null,
	            plotBackgroundImage: null,
	            plotBorderWidth: 0,
	            plotShadow: false,
                height : " + Width.ToString() + @",
                width : " + Height.ToString() + @"
	        },
	    
	        title: {
	            text: '" + Title + @"'
	        },
	    
	        pane: {
	            startAngle: -150,
	            endAngle: 150
	        },
            exporting: {
                buttons: {
                    exportButton: {
                        enabled : false
                    },
                    printButton: {
                        enabled : false
                    }
                }
            },
	       
	        // the value axis
	        yAxis: {
	            min: " + (0).ToString("0.00") + @",
	            max: " + (max + step).ToString("0.00") + @",
	        
	            minorTickInterval: 'auto',
	            minorTickWidth: 1,
	            minorTickLength: 5,
	            minorTickPosition: 'inside',
	            minorTickColor: '#666',
	
	            tickPixelInterval: 30,
	            tickWidth: 2,
	            tickPosition: 'inside',
	            tickLength: 10,
	            tickColor: '#666',
	            labels: {
	                step: 5
	            },
	            title: {
	                text: '" + GaugeUnit + @"'
	            }";

            if (target != alarm && (target != 0 || alarm != 0))
                script += @",
  	            plotBands: [{
  	                from: " + redBegin.ToString("0.00") + @",
  	                to: " + redEnd.ToString("0.00") + @",
  	                color: '#DF5353' // red
  	            }, {
  	                from: " + yellowBegin.ToString("0.00") + @",
  	                to: " + yellowEnd.ToString("0.00") + @",
  	                color: '#DDDF0D' // yellow
  	            }, {
  	                from: " + greenBegin.ToString("0.00") + @",
  	                to: " + greenEnd.ToString("0.00") + @",
  	                color: '#55BF3B' // green
  	            }]";

            script += @"
	        },	
	        series: [{
	            name: '" + Title + @"',
	            data: [" + ActualValue.ToString("0.00") + @"],
	            tooltip: {
	                valueSuffix: ' " + GaugeUnit + @"'
	            }
	        }]	
	};
if ($('#content_" + innerId + @"').size() == 0){
    alert('gauge not found!');
    return;
}
                var gauge_" + ClientID + " = new Highcharts.Chart(option_" + ClientID + ");";

            ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), script, true);
        }

        protected string innerId = Guid.NewGuid().ToString();
    }
}